﻿using B.S.Medical.Api.Write.Application.Command.Pay;
using B.S.Medical.Domain.Payment;
using B.S.Medical.ErrorCode;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Domain;
using Essensoft.Paylink.Alipay.Request;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Text;

namespace B.S.Medical.Api.Write.Controllers.Pay
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AlipayController : ControllerBase
    {
        private readonly ILogger<AlipayController> _logger;
        private readonly IAlipayClient _client;
        private readonly IOptions<AlipayOptions> _optionsAccessor;
        private readonly IMediator mediator;

        public AlipayController(ILogger<AlipayController> logger, IAlipayClient client, IOptions<AlipayOptions> optionsAccessor, IMediator mediator)
        {
            _logger = logger;
            _client = client;
            _optionsAccessor = optionsAccessor;
            this.mediator = mediator;
        }

        /// <summary>
        /// 手机网站支付
        /// </summary>
        [HttpPost]
        public async Task<IActionResult> WapPay(AlipayTradeWapPayViewModel viewMode)
        {
            var model = new AlipayTradeWapPayModel
            {
                //订单描述
                Body = viewMode.Body,
                //商品名称
                Subject = viewMode.Subject,
                //支付金额
                TotalAmount = viewMode.TotalAmount,
                //订单编号
                OutTradeNo = viewMode.OutTradeNo,
                //产品编码
                ProductCode = "QUICK_WAP_PAY"
            };
            var req = new AlipayTradeWapPayRequest();
            req.SetBizModel(model);
            req.SetNotifyUrl(viewMode.NotifyUrl);
            req.SetReturnUrl(viewMode.ReturnUrl);

            var response = await _client.PageExecuteAsync(req, _optionsAccessor.Value);
            return Content(response.Body, "text/html", Encoding.UTF8);
        }

        /// <summary>
        /// 交易退款
        /// </summary>
        [HttpPost]
        public async Task<ApiResult<int>> Refund(AlipayTradeRefundViewModel viewMode)
        {
            var res = new ApiResult<int>();
            var model = new AlipayTradeRefundModel
            {
                OutTradeNo = viewMode.OutTradeNo,
                //TradeNo = viewMode.TradeNo,
                RefundAmount = viewMode.RefundAmount,
                //OutRequestNo = viewMode.OutRequestNo,
                RefundReason = viewMode.RefundReason
            };

            var req = new AlipayTradeRefundRequest();
            req.SetBizModel(model);

            var response = await _client.ExecuteAsync(req, _optionsAccessor.Value);
            //ViewData["response"] = response.Body;
            //response.Code == "10000"
            //{
            //修改订单状态(已退款)
            var updateCommand = new RefundPaymentCommand
            {
                OrderNo = viewMode.OutTradeNo,
                RefundAmount = decimal.Parse(viewMode.RefundAmount),
                RefundReason = viewMode.RefundReason,
            };
            await mediator.Send(updateCommand);
            res.Code = ApiEnum.Success;
            res.Msg = "成功退款";
            
            return res;
        }

        ///// <summary>
        ///// 处理退款命令
        ///// </summary>
        //[HttpPut]
        //public async Task<ApiResult<bool>> RefundPayment(RefundPaymentCommand request)
        //{
        //    return await mediator.Send(request);
        //}
    }
}
